package com.huixuebao.common.util;

import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.bean.RequestData;
import com.huixuebao.common.bean.ResponseData;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * TODO
 *
 * @version 1.0
 * @date 2020-11-08 14:02
 */
@Slf4j
public class CommonUtil {

    /**
     * 组装响应Data
     * @date 2020-11-08 14:07
     * @param signKey, dataKey]
     * @return com.huixuebao.insure.dto.ResponseData
     */
    public static ResponseData getResponseData(Object object, String signKey, String dataKey) throws Exception {
        ResponseData responseData = new ResponseData(ResultCodeEnum.SUCCESS);

        String jsonData = JsonUtil.getStringData(object);
        // 生成签名
        String signData = RSASign.getSha1Sign(jsonData, signKey);
        // 数据加密
        String encryptedData = AESEncrypt.encrypt(jsonData, dataKey);

        responseData.setData(encryptedData);
        responseData.setSign(signData);

        return responseData;
    }
    /**
     * 解析请求Data
     * @date 2020-11-08 14:43
     * @param clazz, verifySignKey, dataKey]
     * @return T
     */
    public static <T> T resolverRequestData(RequestData requestData, Class<T> clazz,
                                            String verifySignKey, String dataKey) throws Exception{
        String decryptedData = AESEncrypt.decrypt(requestData.getData(), dataKey);
        T dto = JsonUtil.getObjectData(decryptedData, clazz);
        return dto;
    }
}